import cn.hutool.core.codec.Base64;
import cn.hutool.core.date.DateUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import cn.hutool.log.Log;
import cn.hutool.log.dialect.log4j.Log4jLog;
import cn.hutool.log.dialect.log4j2.Log4j2Log;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2024.8.15
 */
public class EncryptDemo {
    Log log = new Log4j2Log(EncryptDemo.class);
    //--- 传入的参数
    private final String secretKey = "LZVZJ31K2C2xxPhehUU6hu6e8Y4LkF0s";


    public static String randomStr(int size) {
        String base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < size; ++i) {
            int number = random.nextInt(base.length());
            sb.append(base.charAt(number));
        }
        return sb.toString();
    }

    String getSignature(String content) {
        return cn.hutool.crypto.digest.MD5.create().digestHex(content, StandardCharsets.UTF_8);
    }

    String qr_s = "LZXSKZK9jSw1owWLPnljFTRNrqx2La2s";
    String qr_n = "NHQxz6cEHIRv39yn";


    public void testRequest() {
        OpenApiReq req = new OpenApiReq();
        req.appId = "CZI2DyKnKt4z8n2KPV5OmClA";
        req.timestamp = System.currentTimeMillis();
        req.nonce = EncryptDemo.randomStr(16);
        req.signature = getSignature(req.appId + req.timestamp + req.nonce + secretKey);
        String reqJson = JSONUtil.toJsonStr(req);
        String signature = Base64.encodeUrlSafe(reqJson);
//
        System.out.println("x-tn:"+signature);
        //mock 接口所需参数
//        for (int i = 0; i < 1000; i++) {

//            Map<String, Object> params = new HashMap<>();
//            params.put("id", 16 + i);
//            params.put("orderId", "订单id");
//            params.put("amount", 1000);
//            params.put("businessType", "餐饮");//酒店、景区
//            params.put("merchantId", "商户id");
//            params.put("merchantName", "商户名字");

//            String qrContent = AESUtil.encrypt((38939861000L + i) + "", qr_s, qr_n);

//            System.out.println(qrContent);
//            System.out.println("\r");
//        }
    }

    private final long epoch = 1722083520000L;
    private int seq = 0;

    public String generateId(Integer scope) {
        long currentTimeMillis = System.currentTimeMillis();
        long timePart = (currentTimeMillis - epoch) % 10000000L;
        long sequence = seq & 4095L;
        long codeNum = timePart << 12 | sequence;
        seq += 1;
        return codeNum + "";
    }


    public static void main(String[] args) {
        EncryptDemo encryptDemo = new EncryptDemo();
        encryptDemo.testRequest();
//        System.out.println(encryptDemo.generateId(1));
    }


    class VO {
        public Long id;
        public Long memberId;
    }

    class OpenApiReq {
        /**
         * 消息产生时间
         */
        public Long timestamp;
        /**
         * payload的唯一标识
         */
        public String nonce;
        /**
         * appId、appKey
         */
        public String appId;
        /**
         * 签名
         */
        public String signature;
    }
}




